import 'package:flutter/material.dart';
import 'package:hello_flutter/demo/dialog/dialog.dart';
import 'package:shared_preferences/shared_preferences.dart';

void main() => runApp(App());

class App extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: HomePage(),
    );
  }
}

final dialogPrefKey = 'disclaimer';

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  final _prefs = SharedPreferences.getInstance();
  Future<bool> _unknow;
  GlobalKey<DisclaimerMsgState> dialogKey;
  @override
  void initState() {
    if (dialogKey == null) {
      dialogKey = GlobalKey<DisclaimerMsgState>();
      // 获取偏好设置存储
      _unknow = _prefs.then((SharedPreferences prefs) {
        return (prefs.getBool(dialogPrefKey) ?? false);
      });
      _unknow.then((bool value) {
        Future.delayed(Duration(seconds: 1), () {
          if (!value) {
            dialogKey.currentState.showAlertDialog(context);
          }
        });
      });
    }
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: Colors.white,
      body: Center(
        child: DisclaimerMsg(state: this, key: dialogKey),
      ),
    );
  }
}
